﻿using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Configuration;
using System.IO;
using System.Web.Core;

namespace Yuanlin.Business
{
    public class RecommendedUserNumberAddControl : YuanlinUserControl
    {
        protected ValuedTextBox numberValuedTextBox;

        protected IButton SaveButton;
        protected IButton BackButton;

        protected override void OnLoad(EventArgs e)
        {
            SaveButton.Attributes["onclick"] = "return CheckValue()";
            SaveButton.Click += new EventHandler(SaveButton_Click);
            BackButton.Click += new EventHandler(BackButton_Click);
        }

        public void BackButton_Click(object sender, EventArgs e)
        {
            Page.Response.Redirect(SiteUrls.Instance().GetRecommendedUserNumberListUrl());
        }

        public void SaveButton_Click(object sender, EventArgs e)
        {
            CompanyGreenNumListRequest request = new CompanyGreenNumListRequest();
            request.GreenNum = this.numberValuedTextBox.Value;
            EntityList companyGreenNums = BusinessManager.GetCompanyGreenNumList(RequestHelper.BuildGetCompanyGreenNumListRequest(request));
            if (companyGreenNums.Count > 0)
            {
                Globals.ShowMessage(this.Page, "ExistCompanyGreenNum", "此号码已经被注册过！");
                return;
            }

            RecommendedUserNumber recommendedUserNumber = new RecommendedUserNumber();

            recommendedUserNumber.Number.Value = this.numberValuedTextBox.Value;

            if (BusinessManager.CreateRecommendedUserNumber(recommendedUserNumber) == -2)
            {
                Globals.ShowMessage(this.Page, "添加失败", "此信息已经存在！");
                return;
            }

            Page.Response.Redirect(SiteUrls.Instance().GetRecommendedUserNumberListUrl());
        }
    }
}